﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;

namespace LIMS.Shared
{
    public class ProductHistoryManagementPresenter : BasePresenter<IProductHistoryManagementPageView>
    {
        private IProductHistoryManagementPageView view;
        private ILogger logger;
        private IProductHistoryManagementService ProductHistoryService;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="view"></param>
        /// <param name="cService"></param>
        /// <param name="cUserService"></param>
        /// <param name="oLogger"></param>
        public ProductHistoryManagementPresenter(IProductHistoryManagementPageView view,
                                        IProductHistoryManagementService p_ProductHistoryService,
                                       ILogger oLogger)
            : base(view)
        {
            this.view = view;
            this.ProductHistoryService = p_ProductHistoryService;
            this.logger = oLogger;
        }
        /// <summary>
        /// Initialize View for GroupManagementPresenter
        /// </summary>
        /// <returns></returns>
        public bool InitializeView()
        {
            try
            {
                view.GridData.Criteria.Clear();
                view.GridData.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = view.GridData;
                view.Products = ProductHistoryService.GetAllProductsHistoryForGrid(ref oGrdData);
                view.GridData = oGrdData;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error while initializing Product view. Error : " + ex.Message, ex);
                return false;
            }

        }

        /// <summary>
        /// Saves group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Save(ProductHistory oProductHistory)
        {
            return ProductHistoryService.Save(oProductHistory);
        }

        /// <summary>
        /// Deletes group
        /// </summary>
        /// <param name="oGroup"></param>
        /// <returns></returns>
        public bool Delete(ProductHistory oProductHistory)
        {
            // mark this Product as deactive
            return ProductHistoryService.Delete(oProductHistory);
        }
        /// <summary>
        /// Edit Group
        /// </summary>
        /// <param name="index"></param>
        public bool Edit(int index)
        {
            try
            {
                view.CurrentIndex = index;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error occured while editing record. Exception : " + ex.Message, ex);
                return false;
            }
        }
        /// <summary>
        /// Search User By User Name
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public void SearchProductByKeyword(string sKeyword)
        {
            try
            {
                view.GridData.Criteria.Clear();
                view.GridData.Criteria.Add(new GVEntityData("Name", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.Or));
                view.GridData.Criteria.Add(new GVEntityData("Location", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.And));
                view.GridData.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = view.GridData;
                List<ProductHistory> myProducts = ProductHistoryService.GetAllProductsHistoryForGrid(ref oGrdData);
                view.GridData = oGrdData;
                view.Products = myProducts;
            }
            catch (Exception ex)
            {
                logger.Error("Error in Searching user using Keyword for :" + sKeyword, ex);
            }
        }
    }
}
